package com.nl.lib_project.view.general

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.constraintlayout.widget.ConstraintLayout
import com.nl.lib_core.tool.coroutines.FunctionTypeIntUnit
import com.nl.lib_core.tool.extends.*
import com.nl.lib_project.R

/** 作者:LiBW 创建日期:2022/6/27 描述:阿尔兹海默症 第一题 卡片 */
@SuppressLint("ViewConstructor")
class AlzheimerQuestionItemView(
    var parentId: Int,
    context: Context,
    attributeSet: AttributeSet? = null,
) : ConstraintLayout(context, attributeSet), View.OnClickListener {
  // 标题
  private var textView: TextView? = null
  private var hitTextView: TextView? = null
  private var imageView: ImageFilterView? = null
  private var startImageView: ImageView? = null
  private var mOnSwitchListener: FunctionTypeIntUnit? = null
  init {
    this.id = parentId
    // 本身
    setBackgroundResource(R.drawable.bg_border_gray_8)
    layoutParams = cLayoutParams(context.dp2px(142f), context.dp2px(77f))
    // 背景图
    imageView = ImageFilterView {
      layoutParams =
          cLayoutParams(context.dp2px(142f), context.dp2px(77f)).apply {
            startToStart = parentId
            topToTop = parentId
          }
      round = 8f
      setPadding(2, 2, 2, 2)
    }
    // 文字
    textView = TextView {
      layout_id = "tv_title"
      layoutParams =
          cWLayoutParams().apply {
            startToStart = parentId
            topToTop = parentId
            setMargins(context.dp2px(8f), context.dp2px(8f), 0, 0)
          }
      setColor(R.color.transparent100_gray)
    }
    // hit
    hitTextView = TextView {
      layoutParams =
          cWLayoutParams().apply {
            startToStart = parentId
            topToBottom = textView?.id!!
            setMargins(context.dp2px(8f), context.dp2px(4f), 0, 0)
          }
      setColor(R.color.transparent100_gray)
    }

    // 星
    startImageView = ImageView {
      layoutParams =
          cLayoutParams(context.dp2px(28f), context.dp2px(20f)).apply {
            endToEnd = parentId
            topToTop = parentId
            setMargins(0, 2, 2, 0)
          }
      setImageResource(R.drawable.select_start_off)
    }
    setOnClickListener(this)
  }

  fun setFlag(select: Boolean) {
    if (select) {
      startImageView?.setImageResource(R.drawable.select_start_on)
    } else {
      startImageView?.setImageResource(R.drawable.select_start_off)
    }
  }
  /** * 设置标题 */
  fun setTitle(string: String) {
    textView?.text = string
  }
  /** * 设置背景图 */
  fun setImageBackground(image: String) {
    val img = context.getIdFromDrawable("adas_${image}")
    imageView?.loadImage(context, img)
  }

  fun setHitTitle(hint:String){
    hitTextView?.text = hint
  }

  /** 是否被选中 */
  fun chooseOrNoCurrentView(position: Int) {
    if (parentId == position) {
      setBackgroundResource(R.drawable.bg_border_bule_8)
      textView?.setColor(R.color.blue_foot_bar_bottom_line)
    } else {
      setBackgroundResource(R.drawable.bg_border_gray_8)
      textView?.setColor(R.color.transparent100_gray)
    }
  }

  override fun onClick(v: View?) {
    mOnSwitchListener?.invoke(parentId)
  }

  fun setOnListener(onClickListener: FunctionTypeIntUnit) {
    this.mOnSwitchListener = onClickListener
  }
}
